/****************************************************************************/
/*xxcstallcpm_e.p                          		                            */
/*COPYRIGHT WININ ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.         */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=NO                                                          */
/*V8:RunMode=Character,Windows                                              */
/*MAINTAINING DATA IN MFG/PRO                                               */
/****************************************************************************/
/* PROCEDURE NAME        : xxcstallcpm_e.p                                   */
/* PROCEDURE TYPE        :                                                  */
/* DESCRIPTION           : Variance allocation control         		        */ 
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/* PROJECT               : BMPT EE Project                                  */
/****************************************************************************/
/*DATE:07/01/19    CREATED BY: GAOYAN                                       */
/****************************************************************************/
									 
using com.qad.eefin.buser.EntitySecurityCheck.

/* DISPLAY TITLE */
{us/mf/mfdtitle.i "a+ "} 
DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable xxcstctrlyear    like xxcstctrl_ctrl.xxcstctrl_year     no-undo.
define variable xxcstctrlper     like xxcstctrl_ctrl.xxcstctrl_per      no-undo.
define variable continue         as logical              no-undo.
define variable xxcstctrl-entity like xxcstctrl_ctrl.xxcstctrl_entity   no-undo.
define variable xxcstctrl-site   like xxcstctrl_ctrl.xxcstctrl_site     no-undo. /*MP01*/
define variable xxcstctrl-ID     like xxcstctrl_ctrl.xxcstctrl_ID       no-undo.
define variable m_tmpstr         as character            no-undo.

/* For calling QAD Financials Data Object to check GL Entity Security */
DEFINE VARIABLE hQADFinancialsLib   AS    HANDLE                        NO-UNDO.
DEFINE VARIABLE EntitySecurity      AS    EntitySecurityCheck           NO-UNDO.
DEFINE VARIABLE valid_entity        LIKE  mfc_logical                   NO-UNDO.
DEFINE VARIABLE BufErrorMessage     AS    CHARACTER                     NO-UNDO.
DEFINE VARIABLE ErrorMessage        AS    CHARACTER                     NO-UNDO.


/* DISPLAY SELECTION FORM */
form

	xxcstctrl-entity  colon 25
	xxcstctrl-site    colon 25 /*MP01*/
	xxcstctrl_ctrl.xxcstctrl_year    colon 25
	xxcstctrl_ctrl.xxcstctrl_per     colon 25
with frame a side-labels width 80  attr-space three-d.

setFrameLabels(frame a:handle).

/* DISPLAY */
view frame a.

mainloop:
REPEAT TRANSACTION ON ERROR UNDO mainloop, RETRY mainloop with frame a:


   
   FIND FIRST glc_cal NO-LOCK WHERE glc_domain = global_domain
                                AND glc_start <= TODAY
                                AND glc_end   >= TODAY NO-ERROR.
   IF AVAILABLE glc_cal THEN .

   
   CLEAR FRAME a NO-PAUSE.
   
   UPDATE xxcstctrl-entity   
   WITH FRAME a EDITING:
          
      {us/mf/mfnp05.i en_mstr en_entity "en_domain = global_domain" 
                      en_entity "input xxcstctrl-entity "}
      IF recno <> ? THEN DO:
         ASSIGN xxcstctrl-entity = en_entity.
         DISPLAY
            xxcstctrl-entity
         WITH FRAME a.
      END.         
   END. /* UPDATE EDITING xxcstctrl-entity */
   
   
   IF NOT CAN-FIND( FIRST en_mstr WHERE en_domain = global_domain 
                                   AND en_entity = xxcstctrl-entity)
   THEN DO:
      /* Invalid xxcstctrl-entity */
      {us/bbi/pxmsg.i &MSGNUM=3061 &ERRORLEVEL=3}
      NEXT-PROMPT xxcstctrl-entity WITH FRAME a.
      UNDO, RETRY.
   END. /* IF NOT CAN-FIND(FIRST */
      
   /* CHECK ENTITY SECURITY */
   IF xxcstctrl-entity <> "" THEN DO:    
      {us/gl/glenchk.i &entity=xxcstctrl-entity &entity_ok=valid_entity} 
      IF NOT valid_entity THEN DO:
         NEXT-PROMPT xxcstctrl-entity WITH FRAME a.
         UNDO, RETRY.
      END.     
   END. /* IF xxcstctrl-entity <> "" */
   
   siloop:
   DO ON ERROR UNDO siloop, RETRY siloop:
      
      UPDATE xxcstctrl-site     
           WITH FRAME a EDITING:
         
         {us/mf/mfnp05.i si_mstr si_site "si_domain = global_domain" 
                            si_site "input xxcstctrl-site"}
            IF recno <> ? THEN DO:
               ASSIGN xxcstctrl-site = si_site.
               DISPLAY xxcstctrl-site WITH FRAME a.
            END.            
      END. /* UPDATE EDITING xxcstctrl-site */
      
      IF NOT CAN-FIND( FIRST si_mstr WHERE si_domain = global_domain 
                                      AND si_site   = xxcstctrl-site
									  AND si_entity = xxcstctrl-entity) 
      THEN DO:
         /* Invalid xxcstctrl-site */
         {us/bbi/pxmsg.i &msgnum=2797 &errorlevel=3 }
         NEXT-PROMPT xxcstctrl-site WITH FRAME a.
         UNDO siloop, RETRY siloop.
      END.

      /* xxcstctrl-site Security Check */
      {us/bbi/gprun.i ""gpsiver.p"" "(INPUT xxcstctrl-site,?,OUTPUT li_access)"}
      IF li_access = 0 THEN DO:
         /* User does not have access to this xxcstctrl-site */
         {us/bbi/pxmsg.i &MSGNUM=725 &ERRORLEVEL=3}
         NEXT-PROMPT xxcstctrl-site.
         UNDO siloop, RETRY siloop.
      END. /* IF li_access = 0 */      
   END. /* siloop */
   


for first xxcstctrl_ctrl where xxcstctrl_ctrl.xxcstctrl_domain = global_domain 
                           and xxcstctrl_ctrl.xxcstctrl_entity = xxcstctrl-entity 
			               and xxcstctrl_ctrl.xxcstctrl_site = xxcstctrl-site 
                           and xxcstctrl_ctrl.xxcstctrl_ID     = xxcstctrl-ID  :
end. /*for first xxcstctrl_ctrl.xxcstctrl_file*/

if not available xxcstctrl_ctrl then 
do:
	create xxcstctrl_ctrl.
	assign   
	   xxcstctrl_ctrl.xxcstctrl_domain = global_domain
	   xxcstctrl_ctrl.xxcstctrl_ID     = xxcstctrl-ID 
       xxcstctrl_ctrl.xxcstctrl_entity = xxcstctrl-entity
	   xxcstctrl_ctrl.xxcstctrl_site   = xxcstctrl-site 
	   xxcstctrl_ctrl.xxcstctrl_year   = glc_year
	   xxcstctrl_ctrl.xxcstctrl_per    = glc_per
	   xxcstctrl_ctrl.xxcstctrl_userid = global_userid
	   xxcstctrl_ctrl.xxcstctrl_date   = today
	   xxcstctrl_ctrl.xxcstctrl_time   = time.
end. /*if not available xxcstctrl_ctrl.xxcstctrl_file*/
assign 
  xxcstctrlyear = xxcstctrl_ctrl.xxcstctrl_year
  xxcstctrlper  = xxcstctrl_ctrl.xxcstctrl_per.

update
  xxcstctrl_ctrl.xxcstctrl_year 
  xxcstctrl_ctrl.xxcstctrl_per 
with frame a.


if xxcstctrlyear = xxcstctrl_ctrl.xxcstctrl_year then
do: 

	if xxcstctrl_ctrl.xxcstctrl_per = xxcstctrlper then 
	
		for each xxcstall_hist where xxcstall_domain = global_domain
		                        and  xxcstall_site   = xxcstctrl-site 
					            and  xxcstall_entity = xxcstctrl-entity
					            and  xxcstall_year   = xxcstctrl_ctrl.xxcstctrl_year
					            and  xxcstall_per    = xxcstctrl_ctrl.xxcstctrl_per:
				delete xxcstall_hist.
		end. /*for each xxcstall_hist*/
    
	if xxcstctrl_ctrl.xxcstctrl_per > xxcstctrlper then 
	do:
	
			for last xxcstall_hist where xxcstall_domain  = global_domain
			                         and xxcstall_site    = xxcstctrl-site 
						             and xxcstall_entity  = xxcstctrl-entity
						             and xxcstall_year    = xxcstctrlyear 
						             and xxcstall_per     < xxcstctrlper 
									 no-lock :
			end.								  
			if available xxcstall_hist then 
			do: 
			   
			     assign m_tmpstr = getTermLabel("Period", 8) + " " + string(xxcstall_year) + " " +  string(xxcstall_per) .
				 {us/bbi/pxmsg.i &MSGNUM=3134  &msgarg1=m_tmpstr &ERRORLEVEL=4 &PAUSEAFTER }
				 undo,
				next mainloop.
			end. /*if available xxcstall_hist*/
            /*
			leave.
            */
		
	end. /*if xxcstctrl_ctrl.xxcstctrl_per > xxcstctrlper*/
	else if xxcstctrlper <> xxcstctrl_ctrl.xxcstctrl_per then 
	do:
			continue = no.
	       assign m_tmpstr = string(xxcstctrl_ctrl.xxcstctrl_year) + " " +  string(xxcstctrl_ctrl.xxcstctrl_per) .
	       {us/bbi/pxmsg.i &MSGNUM=8266  &msgarg1=m_tmpstr &ERRORLEVEL= 1 &CONFIRM=continue }
	       
	       if continue = no then
	       do:
					   undo,
					   next mainloop.
		   end. /*if continue = no */
		    else 
		    do: 
		    	for each xxcstall_hist where xxcstall_domain = global_domain 
		                                and  xxcstall_site   = xxcstctrl-site 
		    			                and  xxcstall_entity = xxcstctrl-entity
		    			                and  xxcstall_year   = xxcstctrlyear 
		    			                and  xxcstall_per   >= xxcstctrl_ctrl.xxcstctrl_per :
										/*
						                status default xxcstall_part.
										*/
		    			                delete xxcstall_hist.
		    	end. /*for each xxcstall_hist where*/
				
			    for each xxcstwoallhist_hist where xxcstwoallhist_domain = global_domain 
		                                      and  xxcstwoallhist_site   = xxcstctrl-site 
		    			                      and  xxcstwoallhist_entity = xxcstctrl-entity
		    			                      and  xxcstwoallhist_year   = xxcstctrlyear 
		    			                      and  xxcstwoallhist_per   >= xxcstctrl_ctrl.xxcstctrl_per :
						/*
						status default xxcstwoallhist_comp.
						*/
		    			delete xxcstwoallhist_hist.
				         
		       end. 
			   
		    end.
	end. /*else if xxcstctrlper <> xxcstctrl_ctrl.xxcstctrl_per*/
end. /*if xxcstctrlyear = xxcstctrl_ctrl.xxcstctrl_year*/
if xxcstctrlyear > xxcstctrl_ctrl.xxcstctrl_year  and xxcstctrl_ctrl.xxcstctrl_per = 12 then do:

			continue = no.
	       assign m_tmpstr = string(xxcstctrl_ctrl.xxcstctrl_year) + " " +  string(xxcstctrl_ctrl.xxcstctrl_per) .
	       {us/bbi/pxmsg.i &MSGNUM=8266  &msgarg1=m_tmpstr &ERRORLEVEL=2 &CONFIRM=continue }
	       
	       if continue = no then
	       do:
					   undo,
					   next mainloop.
		   end. /*if continue = no */
		    else 
		    do: 
		    	for each xxcstall_hist where xxcstall_domain = global_domain 
		                                and  xxcstall_site   = xxcstctrl-site 
		    			                and  xxcstall_entity = xxcstctrl-entity
		    			                and  xxcstall_year   = xxcstctrlyear - 1 /* bug fixed yfpo 2020/1/2 */
		    			                and  xxcstall_per    = 12:
										/*
						                status default xxcstall_part.
										*/
		    			                delete xxcstall_hist.
		    	end. /*for each xxcstall_hist where*/
				
			    for each xxcstwoallhist_hist where xxcstwoallhist_domain = global_domain 
		                                      and  xxcstwoallhist_site   = xxcstctrl-site 
		    			                      and  xxcstwoallhist_entity = xxcstctrl-entity
		    			                      and  xxcstwoallhist_year   = xxcstctrlyear - 1 /* bug fixed yfpo 2020/1/2 */
		    			                      and  xxcstwoallhist_per    = 12 :
									/*
						status default xxcstwoallhist_comp.
						*/
		    			delete xxcstwoallhist_hist.
				
		       end. 
			   
		    end.



end. 
if xxcstctrlyear > xxcstctrl_ctrl.xxcstctrl_year  and xxcstctrl_ctrl.xxcstctrl_per <> 12 then 
 do:

    assign m_tmpstr = getTermLabel("Period", 8) + " " + string(xxcstctrl_ctrl.xxcstctrl_year) + " " +  string(xxcstctrl_ctrl.xxcstctrl_per) .
				 {us/bbi/pxmsg.i &MSGNUM=3134  &msgarg1=m_tmpstr &ERRORLEVEL=4 &PAUSEAFTER }
                        undo,
					   next mainloop.
					   

end.
end. /* mainloop */
